'use strict';

const Controller = require('egg').Controller;
const fs = require('mz/fs'); // 引入node的文件处理模块
const path = require('path'); // 引入node的路径处理模块
const pump = require('mz-modules/pump'); //  引入图片存储模块
const utils = require('utility'); // 引入 utils工具库 模块

class HomeController extends Controller {

  async uploadoss() {
    const { ctx } = this;
    const file = ctx.request.files;
    try {
      const reslist = [];
      for (const item of file) {
        const ext = item.filename.split('.')
          .pop();
        const _date = utils.YYYYMMDD('');
        const normalPath = `blog/${_date}`; // 根目录
        const newFileName = Date.now() + '.' + ext;

        try {
          await ctx.oss.put(normalPath + '/' + newFileName, item.filepath);
          const urls = 'http://39.99.143.114:9999/' + normalPath + '/' + newFileName;
          reslist.push(urls);
        } catch (e) {
          ctx.body = { success: false, data: e };
        } finally {
          await fs.unlinkSync(item.filepath);
        }
      }
      ctx.body = { success: true, data: reslist };
    } catch (e) {
      ctx.body = { success: false, msg: '上传失败' };
    }
  }

  async picturecreat() {
    const { ctx } = this;
    // photourl 为 数组字符串
    const { releaseid, photourl, appointmentid, fieldid, tutorialid } = ctx.request.body;
    if (!photourl) {
      return ctx.body = { success: false, msg: '未上传地址' };
    }
    if (!(releaseid || appointmentid || fieldid || tutorialid)) {
      return ctx.body = { success: false, msg: '请传入图片的类型' };
    }
    const create = {};
    if (releaseid) {
      create.releaseid = releaseid;
    }
    if (appointmentid) {
      create.appointmentid = appointmentid;
    }
    if (fieldid) {
      create.fieldid = fieldid;
    }
    if (tutorialid) {
      create.tutorialid = tutorialid;
    }
    const urlarray = photourl.split(',');
    try {
      for (const item of urlarray) {
        create.photourl = item;
        await ctx.model.Picture.create({
          create,
        });
      }
      ctx.body = { success: true, msg: '上传成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '上传失败' };
    }
  }

  async pictureupdate() {
    const { ctx } = this;
    const { photoid, releaseid, appointmentid, fieldid, tutorialid, photourl } = ctx.request.body;
    if (!(photoid || releaseid || appointmentid || fieldid || tutorialid)) {
      return ctx.body = { success: false, msg: '必须传入查找字段' };
    }
    if (!photourl) {
      return ctx.body = { success: false, msg: '必须传入图片' };
    }
    const where = {};
    if (photoid) {
      where.photoid = photoid;
    }
    if (releaseid) {
      where.releaseid = releaseid;
    }
    if (appointmentid) {
      where.appointmentid = appointmentid;
    }
    if (fieldid) {
      where.fieldid = fieldid;
    }
    if (tutorialid) {
      where.tutorialid = tutorialid;
    }
    try {
      await ctx.model.Picture.update({
        photourl,
      },
      where
      );
      ctx.body = { success: true, msg: '修改成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '修改失败' };
    }
  }

}

module.exports = HomeController;
